package net.jueb.frameworkDemo.orm.mongodb.util;

import java.util.Arrays;

import org.bson.codecs.Codec;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
public class MongoUtil {

	
	public static MongoClient buildClient(String host,int port,String authDataBaseName,String user,String pwd,MongoClientOptions.Builder builder) {
		//创建认证信息
		MongoCredential auth=MongoCredential.createCredential(user,authDataBaseName, pwd.toCharArray());
		return new MongoClient(new ServerAddress(host,port),Arrays.asList(auth),builder.build());//获取客户端
	}
	
	public static MongoClient buildClient(String host,int port,String authDataBaseName,String user,String pwd) {
		//创建认证信息
		MongoCredential auth=MongoCredential.createCredential(user,authDataBaseName, pwd.toCharArray());
		return new MongoClient(new ServerAddress(host,port),Arrays.asList(auth));//获取客户端
	}
	
	public static MongoClient buildClient(String host,int port) {
		return new MongoClient(new ServerAddress(host,port));//获取客户端
	}
	
	public static MongoClient buildClient(MongoClientURI uri)
	{
		return new MongoClient(uri);
	}
	
	public static MongoClient buildClient(String uri)
	{
		return new MongoClient(new MongoClientURI(uri));
	}
	
	public static MongoClientURI buildUrl(String host,int port,String authDataBaseName,String user,String pwd,MongoClientOptions.Builder builder)
	{
		return new MongoClientURI("mongodb://"+user+":"+pwd+"@"+host+":"+port+"/?authSource="+authDataBaseName,builder);
	}
	
	public static MongoClientURI buildUrl(String host,int port,String authDataBaseName,String user,String pwd)
	{
		return new MongoClientURI("mongodb://"+user+":"+pwd+"@"+host+":"+port+"/?authSource="+authDataBaseName);
	}
	
	public static MongoClientOptions.Builder getDefaultBuilder(CodecRegistry codecRegistry)
	{
		MongoClientOptions.Builder build = new MongoClientOptions.Builder();  
		//与数据最大连接数10  
		build.connectionsPerHost(10);  
		//如果当前所有的connection都在使用中，则每个connection上可以有50个线程排队等待  
		build.threadsAllowedToBlockForConnectionMultiplier(10);  
		build.connectTimeout(1*60*1000);  
		build.maxWaitTime(60*60*1000);  
		build.codecRegistry(codecRegistry);
		return build;
	}
	
	public static void setCodecRegistry(MongoDatabase database,CodecRegistry codecRegistry)
	{
		//合并后的解码器集合
		CodecRegistry newCodecRegistry = CodecRegistries.fromRegistries(database.getCodecRegistry(),codecRegistry); 
		//设置解码器集合
		database.withCodecRegistry(newCodecRegistry);
	}
	
	public static CodecRegistry buildCodecRegistry(final Codec<?>... codecs)
	{
		//注册枚举类型编码解码器集合
		CodecRegistry enumCodecRegistry=CodecRegistries.fromCodecs(codecs);
		//默认的解码器集合
		CodecRegistry defaultCodecRegistry=MongoClient.getDefaultCodecRegistry();
		//合并后的解码器集合
		CodecRegistry newCodecRegistry = CodecRegistries.fromRegistries(enumCodecRegistry,defaultCodecRegistry); 
		return newCodecRegistry;
	}
}
